package team.star.werespos.controller;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import team.star.werespos.exception.GlobalException;
import team.star.werespos.model.vo.ResultVO;
import team.star.werespos.model.entity.Role;
import team.star.werespos.model.enums.CodeMsgEnum;
import team.star.werespos.service.RoleService;
import team.star.werespos.util.Result;

import java.util.List;

/**
 * RoleController class<br/>
 *
 * @author CAI
 * @since 2019/04/09
 */
@Slf4j
@RequestMapping("/admin/role")
@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/{rid}/profile")
    public ResultVO<?> getProfile(@PathVariable(name = "rid") Integer rid) {
        Role role = roleService.getOne(rid);
        if (role == null) {
            log.info("【查看角色】 角色不存在 rid = {}, CodeMsgEnum.ROLE_NOT_EXIST = {}",
                    rid, CodeMsgEnum.ROLE_NOT_EXIST);
            throw GlobalException.create(CodeMsgEnum.ROLE_NOT_EXIST);
        }
        return Result.success(role);
    }

    @PostMapping("/new")
    public ResultVO<?> newRole(@RequestBody Role role) {
        roleService.insertOne(role);
        return Result.success();
    }

    /**
     * 获取角色列表
     *
     * @return
     */
    @GetMapping("/list")
    public ResultVO<List<Role>> Role() {
        return Result.success(
                roleService.listRole()
        );
    }

    /**
     * 修改角色信息
     */
    @PostMapping("/{rid}/update")
    public ResultVO<?> updateById(@PathVariable Integer rid,
                                  @RequestBody Role role) {
        role.setRid(rid);
        roleService.updateById(role);
        return Result.success();
    }

    /**
     * 删除角色
     */
    @GetMapping("/{rid}/delete")
    public ResultVO<?> deleteById(@PathVariable Integer rid) {
        roleService.deleteById(
                new Role().setRid(rid)
        );
        return Result.success();
    }
}
